<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">

          <el-col :lg="6" :md="12">
            <el-form-item label="名称:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="分类:">
              <el-select v-model="where.likes" placeholder="相似度">
                <el-option label="全部" value="0"></el-option>
                <el-option label="相似度<70%" value="1"></el-option>
                <el-option label="70%<相似度<80%" value="2"></el-option>
                <el-option label="80%<相似度<90%" value="3"></el-option>
                <el-option label="相似度>90%" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12" >
            <el-form-item label="时间:">
              <el-date-picker
                v-model="searchTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col >
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-download"
            class="ele-btn-icon"
            @click="exportExcel()"
          >导出
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:identifydata:dall')">删除
          </el-button>
        </template>
        <template slot="likes" slot-scope="{row}">

          {{parseFloat(row.likes*100).toFixed(2)}}%
        </template>
        <template slot="image" slot-scope="{row}">
          <el-image  :src="row.dentify.url"/>
        </template>
        <template slot="status" slot-scope="{row}">
          <ele-dot :type="['danger', 'success'][row.status]" :ripple="row.status===0"
                   :text="['待审核','已审核'][row.status]"/>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:identifydata:edit')">审核
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此识别记录数据表吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:identifydata:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <IdentifyDataEdit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
      />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import IdentifyDataEdit from './identifydata-edit';
import * as XLSX from "xlsx";

export default {
  name: 'IdentifyData',
  components: {IdentifyDataEdit},

  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/identifydata/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'name',
          label: '名称',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          columnKey: 'image',
          label: '识别图片',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'image'
        },
        {
          columnKey: 'likes',
          label: '相似度',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot: 'likes'
        },

        {
          prop: 'dentify.createTime',
          label: '识别时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
          slot: 'status'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {likes:'0'},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      searchTime: [],
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      if(this.searchTime.length>0){
        this.where.time = this.searchTime.toString();
      }
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.searchTime=[];
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row == null ? {status: 1} : row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      console.log(row)
      const loading = this.$loading({lock: true});
      this.$http.delete('/identifydata/delete/' + [row.id]).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的识别记录数据表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/identifydata/delete/' + [this.selection.map(d => d.id)]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 导出数据 */
    exportExcel() {
      const loading = this.$loading({lock: true});
      let array = [['ID','名称','识别图片','相似度','识别时间','经纬度','状态',]];
      // 请求查询全部(不分页)的接口
      //const loading = this.$loading({lock: true});
      let where = "&page=1";
      if(this.where.status && this.where.status!=undefined){
        where+='&status='+this.where.status
      }
      if(this.likes && this.where.likes!=undefined){
        where+='&likes='+this.where.likes
      }else{
        where+='&likes=0'
      }
      if(this.searchTime && this.searchTime.length>0 && this.searchTime!=undefined){
        where+='&time='+this.searchTime.toString()
      }

      this.$http.get('/identifydata/index?limit=-1'+where

      ).then(res => {
        loading.close();
        if (res.data.code === 0) {

          res.data.data.records.forEach(d => {
            array.push([
              d.id,
              d.name,
              d.dentify.url,
              parseFloat(d.likes*100).toFixed(2)+'%',
              d.dentify.createTime,
              d.dentify.latitude+','+d.dentify.longitude,
              d.status==0?'待审核':'已审核',

            ]);
          });

          let sheet = XLSX.utils.aoa_to_sheet(array);
          let sheetname = '识别记录';
          let type = 'xlsx';
          if (Array.isArray(sheet)) sheet = XLSX.utils.aoa_to_sheet(sheet);
          let workbook = {SheetNames: [sheetname], Sheets: {}};

          workbook.Sheets[sheetname] = sheet;
          XLSX.writeFile(workbook, sheetname + '.' + type);

          //this.$util.exportSheet(XLSX, sheet, '空投名单');

        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
